home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2001 / MacHack 2001.toast / pc / The Hacks / Airport Radar™ / Source / RadarView.cp < prev    next >
Encoding:
Text File  |  2001-06-23  |  4.7 KB  |  201 lines

  1.  
  2. #include "RadarView.h"
  3. #include <fp.h>
  4. #include "BaseStation.h"
  5.  
  6. const UInt32        kLocDotRadius        = 10;
  7. const UInt32        kTotalPhosphorSteps    = 16;
  8. const UInt32        kDegreesPerStep        = 2;
  9. const double        kPi                    = 3.14159;
  10.  
  11. #define DRAW_INTERSECT_POINTS 0
  12.  
  13. static BaseStationList        sBaseStationList;
  14.  
  15. RadarView::RadarView(
  16.     LStream *    inStream)
  17.     :    LOffscreenView(inStream),
  18.         LPeriodical(),
  19.         mRadarMapPict(NULL),
  20.         mLastLocationUpdateTime(0),
  21.         mLastSweepUpdateTime(0)
  22. {
  23.     StartIdling();
  24.     
  25.     mCurrentLocation.h = 0;
  26.     mCurrentLocation.v = 0;
  27.     mPreviousLocation.h = 0;
  28.     mPreviousLocation.v = 0;
  29.     
  30.     mSweepAngle = 0.0;
  31. }
  32.         
  33. RadarView::~RadarView()
  34. {
  35. }
  36.  
  37.  
  38. void
  39. RadarView::FinishCreateSelf()
  40. {
  41.     Rect bounds;
  42.     CalcLocalFrameRect(bounds);
  43.     mCenterOfWindow.h = bounds.right / 2;
  44.     mCenterOfWindow.v = bounds.bottom / 2;
  45. }
  46.  
  47.  
  48. void
  49. RadarView::DrawSelf()
  50. {
  51.     LOffscreenView::DrawSelf();
  52.  
  53.     // Draw the PICT first
  54.     if (mRadarMapPict == NULL)
  55.         mRadarMapPict = ::GetPicture(128);
  56.     
  57.     if (mRadarMapPict != NULL)
  58.     {
  59.         Rect        boundingRect;
  60.         
  61.         boundingRect = mRadarMapPict[0]->picFrame;
  62.         
  63.         ::DrawPicture(mRadarMapPict, &boundingRect);
  64.         
  65.         RGBColor redRGBColor = {0xC000, 0x0000, 0x0000};
  66.         
  67.         ::RGBForeColor(&redRGBColor);
  68.         
  69.         ::SetRect(&boundingRect, mCurrentLocation.h - kLocDotRadius,
  70.                 mCurrentLocation.v - kLocDotRadius,
  71.                 mCurrentLocation.h + kLocDotRadius,
  72.                 mCurrentLocation.v + kLocDotRadius);
  73.         
  74.         ::PaintOval(&boundingRect);
  75.         
  76.         ::MoveTo(mCurrentLocation.h + kLocDotRadius, mCurrentLocation.v - kLocDotRadius);
  77.         ::DrawString("\pYou Are Here");
  78.         
  79.         Rect bounds;
  80.         CalcLocalFrameRect(bounds);
  81.  
  82.         ::InsetRect(&bounds, -1000, -1000);
  83.  
  84.         // Draw the trailing phosphors
  85.         SInt32 curAngleInDegrees = mSweepAngle / kPi * 180 + 90 - kDegreesPerStep * kTotalPhosphorSteps;
  86.         for (UInt32 phosphorStep = 0; phosphorStep < kTotalPhosphorSteps; phosphorStep++)
  87.         {
  88.             if (curAngleInDegrees >= 360)
  89.                 curAngleInDegrees -= 360;
  90.             RGBColor        notGreenColor = {phosphorStep * 0x8000 / kTotalPhosphorSteps, 
  91.                                             0x0000, 
  92.                                             phosphorStep * 0x8000 / kTotalPhosphorSteps};
  93.             ::RGBForeColor(¬GreenColor);
  94.             ::PenMode(subPin);
  95.             ::PaintArc(&bounds, curAngleInDegrees, kDegreesPerStep);
  96.             
  97.             curAngleInDegrees += kDegreesPerStep;
  98.         }
  99.  
  100.         // Draw the sweeping Radar line
  101.         ::PenSize(2, 2);
  102.         ::PenMode(srcCopy);
  103.  
  104.         curAngleInDegrees -= 90.0;
  105.         ::MoveTo(mCenterOfWindow.h, mCenterOfWindow.v);
  106.         ::ForeColor(greenColor);
  107.         ::Line(4000.0 * cos((double)curAngleInDegrees * kPi / 180.0), 4000.0 * sin((double)curAngleInDegrees * kPi / 180.0));
  108.         
  109.         ::PenNormal();
  110.         ::ForeColor(blackColor);
  111.         
  112.         // Display circles to show strength
  113.         RGBColor grayColor = {0x2000, 0x2000, 0x2000};
  114.         ::RGBForeColor(&grayColor);
  115.         ::PenMode(subPin);
  116.         
  117.         for (UInt32 baseIndex = 0; baseIndex < sBaseStationList.fBaseCount; baseIndex++)
  118.         {
  119.             if (sBaseStationList.fBases[baseIndex].fStrongSignal)
  120.             {
  121.                 Rect    radiusBox;
  122.                 
  123.                 ::SetRect(&radiusBox, 
  124.                     sBaseStationList.fBases[baseIndex].fX - sBaseStationList.fBases[baseIndex].fR,
  125.                     sBaseStationList.fBases[baseIndex].fY - sBaseStationList.fBases[baseIndex].fR,
  126.                     sBaseStationList.fBases[baseIndex].fX + sBaseStationList.fBases[baseIndex].fR,
  127.                     sBaseStationList.fBases[baseIndex].fY + sBaseStationList.fBases[baseIndex].fR);
  128.                 
  129.                 ::PaintOval(&radiusBox);
  130.             }
  131.         }
  132.         
  133. #if DRAW_INTERSECT_POINTS
  134.         for (UInt32 sectPoint = 0; sectPoint < 3; sectPoint++)
  135.         {
  136.             Rect    sampleBox;
  137.             
  138.             ::SetRect(&sampleBox,
  139.                 sBaseStationList.fSectPoints[sectPoint].fPoint1.fX - 2,
  140.                 sBaseStationList.fSectPoints[sectPoint].fPoint1.fY - 2,
  141.                 sBaseStationList.fSectPoints[sectPoint].fPoint1.fX + 2,
  142.                 sBaseStationList.fSectPoints[sectPoint].fPoint1.fY + 2);
  143.             
  144.             ::PaintOval(&sampleBox);
  145.  
  146.             ::SetRect(&sampleBox,
  147.                 sBaseStationList.fSectPoints[sectPoint].fPoint2.fX - 2,
  148.                 sBaseStationList.fSectPoints[sectPoint].fPoint2.fY - 2,
  149.                 sBaseStationList.fSectPoints[sectPoint].fPoint2.fX + 2,
  150.                 sBaseStationList.fSectPoints[sectPoint].fPoint2.fY + 2);
  151.             
  152.             ::PaintOval(&sampleBox);
  153.         }
  154. #endif
  155.     }
  156. }
  157.  
  158. void
  159. RadarView::SpendTime(
  160.     const EventRecord &    inMacEvent)
  161. {
  162.     // Has enough time passed yet? If so, recalculate and
  163.     // refresh the screen.
  164.     if (inMacEvent.when > mLastLocationUpdateTime + 60)
  165.     {
  166.         UpdateLocation();
  167.         mLastLocationUpdateTime = inMacEvent.when;
  168.     }
  169.  
  170.     if (inMacEvent.when != mLastSweepUpdateTime)
  171.     {
  172.         mSweepAngle += kPi / 64.0;
  173.         if (mSweepAngle > 2 * kPi)
  174.             mSweepAngle -= 2 * kPi;
  175.         
  176.         mLastSweepUpdateTime = inMacEvent.when;
  177.         Refresh();
  178.     }
  179. }
  180.  
  181.  
  182. void
  183. RadarView::UpdateLocation()
  184. {
  185.     sBaseStationList.IdleBases();
  186.  
  187.     Point prev = mPreviousLocation;
  188.  
  189.     mPreviousLocation.h = (SInt16)sBaseStationList.fX;
  190.     mPreviousLocation.v = (SInt16)sBaseStationList.fY;
  191.  
  192.     // Average previous and current
  193.     mCurrentLocation.h = (mPreviousLocation.h + prev.h) / 2;
  194.     mCurrentLocation.v = (mPreviousLocation.v + prev.v) / 2;
  195.     
  196. }
  197.  
  198.  
  199.  
  200.  
  201.